/*
 * 
 */
package ch.gje.playios.communication;

import ch.gje.playios.server.HTTPConnection;

/**
 * Represents an iOS client
 *
 * @author jannis <jannis@gje.ch
 */
public class IOSClient extends WebSocketClient {

    /**
     * Constructor
     *
     * @param connection the HTTP connection
     */
    public IOSClient(HTTPConnection connection) {
        super(connection);
    }

    @Override
    public void onPaired() {
        sendCommand("Paired");
    }

    @Override
    public void onUnpaired() {
        sendCommand("Unpaired");
    }

    @Override
    public void onReceive(String message) {
        if (message.startsWith("/PairingNumber:")) {
            int pairingNumber = Integer.parseInt(message.substring(15));
            WebSocketClient partnerClient = WebSocketPairer.getAndRemoveUnpairedClient(pairingNumber);
            if (partnerClient == null) {
                sendCommand("InvalidPairingNumber");
            } else {
                pairWith(partnerClient);
            }
        } else if (partner != null) {
            partner.send(message);
        }
    }
}
